package com.itheima.mapper;

import com.itheima.po.Customer;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

public class MybatisTest {

    public static void main(String[] args) throws Exception{
        String resouse="mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resouse);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();
       Customer customer=sqlSession.selectOne("com.itheima.mapper.CustomerMapper.findCustomerById",1);
     System.out.println(customer.toString());
     sqlSession.close();
    }
    @Test
    public void findCustomerByNameTest() throws Exception {
        String resouse = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resouse);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<Customer> customers = sqlSession.selectList("com.itheima.mapper.CustomerMapper.findCustomerByName", "j");
        for (Customer customer : customers) {
            System.out.println(customer.toString());
        }
        sqlSession.close();
    }
    @Test
    public void addCustomer() throws Exception{
        String resouse="mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resouse);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        Customer customer=new Customer();
        customer.setUsername("rose");
        customer.setJobs("student");
        customer.setPhone("133333533092");
        int rows=sqlSession.insert("com.itheima.mapper.CustomerMapper.addCustomer",customer);
        if (rows>0) {
            System.out.println("成功插入"+rows+"条数据");
        }else {
            System.out.println("未成功插入数据");
        }
        sqlSession.close();
    }
    @Test
    public void updateCustomer() throws Exception{
        String resouse="mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resouse);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        Customer customer=new Customer();
        customer.setId(4);
        customer.setUsername("rose");
        customer.setJobs("programmer");
        customer.setPhone("131111111");
        int rows=sqlSession.update("com.itheima.mapper.CustomerMapper.updateCustomer",customer);
        if (rows>0) {
            System.out.println("成功修改"+rows+"条数据");
        }else {
            System.out.println("未成功修改数据");
        }
        sqlSession.close();
    }
    @Test
    public void deleteCustomer() throws Exception{
        String resouse="mybatis-config.xml";
        InputStream inputStream= Resources.getResourceAsStream(resouse);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        int rows=sqlSession.delete("com.itheima.mapper.CustomerMapper.deleteCustomer",4);
        if (rows>0) {
            System.out.println("成功删除"+rows+"条数据");
        }else {
            System.out.println("未成功删除数据");
        }
        sqlSession.close();
    }
}
